<?php
/**
 * Created by PhpStorm.
 * User: keke
 * Date: 2020/8/15
 * Time: 17:46
 */

namespace App\Model;

use Illuminate\Support\Facades\DB;

class FinanceRoomPunish extends BaseModel
{
    protected $table = 'finance_room_punish';
    protected $fields = ['finance_room_punish.id','room.room_uid','room.name','finance_room_punish.month','finance_room_punish.ratio'];

    public function getRoomPunishList($request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $roomUid = $request->input('room_uid');
        $roomName = trim($request->input('room_name'));
        $year = $request->input('year');
        $month = $request->input('month');
        $where[] = ['room.name', 'like', "{$roomName}%"];
        if ($roomUid) {
            $where['room.room_uid'] = $roomUid;
        }
        if (!empty($year)) {
            $where[] = ['finance_room_punish.month', 'like', "{$year}%"];
        }
        if (!empty($month)) {
            $where[] = ['finance_room_punish.month', 'like', "%{$month}"];
        }
        if (!empty($year) && !empty($month)) {
            $where['finance_room_punish.month'] = intval($year . $month);
        }

        return DB::connection($this->connection)->table('finance_room_punish')
            ->leftJoin('room', 'finance_room_punish.room_id', '=', 'room.room_id')
            ->where($where)
            ->orderBy('finance_room_punish.id','desc')
            ->paginate($limit,$this->fields,'page',$page);
    }


    public function createRoomPunished($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/finance/settle/createRoomPunish', $data, 'urlencoded', 'POST');
    }


    public function deleteRoomPunished($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/finance/settle/deleteRoomPunish', $data, 'urlencoded', 'POST');
    }
}